package ysq.xyg.host.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.util.BaseUtil;
import ysq.xyg.host.dao.HostSettledDao;
import ysq.xyg.host.model.po.HostSettled;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.host.model.vo.HostSettledVo;
import ysq.xyg.host.service.HostSettledService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 房产模块-报备service实现
 *
 * @author shengQiang yu
 * @date 2019-7-15 10:43:05
 */
@Service("hostSettledService")
public class HostSettledServiceImpl extends BaseServiceImpl<HostSettled> implements HostSettledService {

    @Resource
    private HostSettledDao hostSettledDao;

    private HostSettled hostSettled;

    @Autowired
    public void setBaseDao(HostSettledDao hostSettledDao) {
        this.baseDao = hostSettledDao;
    }

    /**
     * 获取数量
     *
     * @param hostSettledVo 对象
     * @return
     * @author shengQiang yu
     * @date 2019-04-27 9:01
     */
    public int selectCountNum(HostSettledVo hostSettledVo){
        return  hostSettledDao.selectCountNum(hostSettledVo);
    }
    /**
     * 查询集合
     *
     * @param hostAdviseVo 对象
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-5 22:08:52
     */
    public List<HostSettled> listLeft(HostSettledVo hostAdviseVo) {
        List<HostSettled> list = hostSettledDao.listLeft(hostAdviseVo);
        if(list!=null&&list.size()>0){
            for (HostSettled hostAdvise:list){
                if(StrUtil.isNotEmpty(hostAdvise.getNickName())){
                    hostAdvise.setNickName(BaseUtil.decodeBase64(hostAdvise.getNickName()));
                }
            }
        }
        return list;
    }

    /**
     * 分页查询
     *
     * @param hostSettledVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-15 10:43:05
     */
    public ReturnEntity page(HostSettledVo hostSettledVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<HostSettled> list = listLeft(hostSettledVo);
        PageInfo<HostSettled> page=new PageInfo<HostSettled>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param hostSettledVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-15 10:43:05
     */
    public ReturnEntity addOrUpdate(HostSettledVo hostSettledVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(hostSettledVo.getId())){
            hostSettled=new HostSettled(currentUser);
            isUpate=false;
        }else{
            hostSettled=selectByPrimaryKey(hostSettledVo.getId());
            if(ObjectUtil.isNull(hostSettled)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            hostSettled.setUpdateTime(new Date());
            hostSettled.setUpdateUser(currentUser.getId());
        }
        hostSettled.setCheckStatus(hostSettledVo.getCheckStatus());
        hostSettled.setCheckResult(hostSettledVo.getCheckResult());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(hostSettled);
        }else{
            num=insert(hostSettled);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-15 10:43:05
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        HostSettled hostSettled=selectByPrimaryKey(id);
        if(hostSettled!=null){
            hostSettled.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            hostSettled.setUpdateTime(new Date());
            hostSettled.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(hostSettled);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param hostSettledVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-7-15 10:43:05
     */
    public List<HostSettled> acceptList(HostSettledVo hostSettledVo){
        Example example = new Example(HostSettled.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(hostSettledVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",hostSettledVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(hostSettledVo.getMemberId())){
            criteria.andEqualTo("memberId",hostSettledVo.getMemberId());
        }
        if(StringUtil.isNotEmpty(hostSettledVo.getCheckStatus())){
            criteria.andEqualTo("checkStatus",hostSettledVo.getCheckStatus());
        }
        if(StringUtil.isNotEmpty(hostSettledVo.getItemId())){
            criteria.andEqualTo("itemId",hostSettledVo.getItemId());
        }
        example.setOrderByClause(" update_time desc");
        return hostSettledDao.selectByExample(example);
    }
}
